package com.umeng.soexample.mvp.persenter;



import com.umeng.lib_core.mvp.presenter.BasePresenter;
import com.umeng.soexample.bean.BrandGoodsEntity;
import com.umeng.soexample.bean.GoodsListEntity;
import com.umeng.soexample.bean.ShopGoodsEntity;
import com.umeng.soexample.mvp.contract.ShopDetailsFragmentContract;

import org.json.JSONException;
import org.json.JSONObject;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ShopDetailsFragmentPersenter extends BasePresenter<ShopDetailsFragmentContract.ShopDetailsFragmentIView, ShopDetailsFragmentContract.ShopDetailsFragmentIModel> {
    @Inject
    public ShopDetailsFragmentPersenter(ShopDetailsFragmentContract.ShopDetailsFragmentIView shopDetailsFragmentIView, ShopDetailsFragmentContract.ShopDetailsFragmentIModel shopDetailsFragmentIModel) {
        super(shopDetailsFragmentIView, shopDetailsFragmentIModel);
    }
    //商品列表请求数据
    public void goodsListRequest(String categoryid, String page){
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("categoryid", categoryid);
            jsonObject.put("page", page);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        m.goods_list(jsonObject.toString()).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<GoodsListEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        v.showDialog();
                        disposable.add(d);
                    }

                    @Override
                    public void onNext(GoodsListEntity goodsListEntity) {
                        v.hideDialog();
                        v.goodsListSuccess(goodsListEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        v.hideDialog();
                        v.showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        v.hideDialog();
                    }
                });
    }

    //店铺商品请求数据
    public void shopGoodsRequest(String categoryid, String page){
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("categoryid", categoryid);
            jsonObject.put("page", page);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        m.shop_goods(jsonObject.toString()).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ShopGoodsEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        v.showDialog();
                        disposable.add(d);
                    }

                    @Override
                    public void onNext(ShopGoodsEntity shopGoodsEntity) {
                        v.hideDialog();
                        v.shopGoodsSuccess(shopGoodsEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        v.hideDialog();
                        v.showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        v.hideDialog();
                    }
                });
    }

    //品牌商品列表请求数据
    public void brandGoodsRequest(String brand, String page){
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("brand", brand);
            jsonObject.put("page", page);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        m.brand_goods(jsonObject.toString()).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BrandGoodsEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        v.showDialog();
                        disposable.add(d);
                    }

                    @Override
                    public void onNext(BrandGoodsEntity brandGoodsEntity) {
                        v.hideDialog();
                        v.brandGoodsSuccess(brandGoodsEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        v.hideDialog();
                        v.showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        v.hideDialog();
                    }
                });
    }

}
